package cn.yx.framework.util;

import org.apache.commons.lang3.StringUtils;

import java.io.*;

/**
 * Created by Administrator on 2017/3/16.
 */
public class FileUtil {

    public static void writeFile(String path, String name, byte[] fileByte) throws Exception {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();//可创建多级目录，而mkdir()只能创建一级目录
        }


        byte[] bytes = fileByte;
        BufferedOutputStream stream =
                new BufferedOutputStream(new FileOutputStream(new File(dir.getPath() + "/" + name)));
        stream.write(bytes);
        stream.close();


    }

    /**
     * 读取txt文件的内容
     *
     * @param filename 想要读取的文件全路径
     * @return 返回文件内容
     */
    public static String File2String(String filename) {
        File file = new File(filename);
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                result.append(System.lineSeparator() + s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     *
     * @param fileName 文件名
     */
    public static String readFile(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuilder result = new StringBuilder();
        try {

            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String tempString = "";

            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                //System.out.println("line " + line + ": " + tempString);
//                if (StringUtils.isNoneBlank(result)) {
//                    result.append(System.lineSeparator() + tempString);
//                } else {
//                    result.append(tempString);
//                }
                result.append(tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return result.toString();
    }

    public static String getFileName(String fileName) {
        File tempFile = new File(fileName.trim());
        return tempFile.getName();
//
//
//        if (fileName.indexOf(".") >= 0) {
//            return fileName.substring(0, fileName.indexOf("."));
//        } else {
//            return fileName;
//        }
    }

    public static String getExtName(String fileName) {
        File f = new File(fileName);
        return f.getName().substring(f.getName().lastIndexOf(".") + 1);
    }

    // 递归方法
    public static void copyFile(File file, File file2) {
        // 当找到目录时，创建目录
        if (file.isDirectory()) {
            file2.mkdir();
            File[] files = file.listFiles();
            for (File file3 : files) {
                // 递归
                copyFile(file3, new File(file2, file3.getName()));
            }
            //当找到文件时
        } else if (file.isFile()) {
            File file3 = new File(file2.getAbsolutePath());
            try {
                file3.createNewFile();
                copyDatas(file.getAbsolutePath(), file3.getAbsolutePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 复制文件数据的方法
    public static void copyDatas(String filePath, String filePath1) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            // 字节流
            fis = new FileInputStream(filePath);
            fos = new FileOutputStream(filePath1);
            byte[] buffer = new byte[1024];
            while (true) {
                int temp = fis.read(buffer, 0, buffer.length);
                if (temp == -1) {
                    break;
                } else {
                    fos.write(buffer, 0, temp);
                }
            }
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }
}
